package com.qeeyou.apps.android.craftsman.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class SelectImageGridAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater; // 视图容器

    private OnItemClickListener onItemClickListener;

    private List<String> mImagePaths;
    private int mLimit;

    public SelectImageGridAdapter(Context context, List<String> imagePaths, int limit) {
        this.mContext = context;
        this.mLimit = limit;
        this.mImagePaths = makeUrls(imagePaths);
        inflater = LayoutInflater.from(context);
    }

    public void update(List<String> imagePaths) {
        this.mImagePaths = makeUrls(imagePaths);
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public int getCount() {
        return mImagePaths.size();
    }

    public Object getItem(int arg0) {

        return null;
    }

    public long getItemId(int arg0) {
        return 0;
    }

    /**
     * ListView Item设置
     */
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_select_image_grid_item,
                    parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String url = mImagePaths.get(position);

        if (url.contains("drawable")) {
            ImageLoader.getInstance()
                    .displayImageById(mContext, R.drawable.icon_add_image, holder.mImageView);
            holder.mCancelView.setVisibility(View.INVISIBLE);
        } else {
            ImageLoader.getInstance()
                    .displayImage(mContext, url, holder.mImageView);
            holder.mCancelView.setVisibility(View.VISIBLE);
        }

        holder.mCancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemCancelClick(position);
                }
            }
        });

        holder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemImageClick(position);
                }
            }
        });

        return convertView;
    }

    public interface OnItemClickListener {
        void onItemImageClick(int position);

        void onItemCancelClick(int position);
    }


    private List<String> makeUrls(List<String> photoPath) {
        List<String> urls = new ArrayList<String>();
        for (int i = 0; i < photoPath.size(); i++) {
            String url = photoPath.get(i);
            urls.add(url);
        }
        if (photoPath.size() < mLimit) {
            String url = "drawable://" + R.drawable.icon_add_image;
            urls.add(url);
        }
        return urls;
    }

    static class ViewHolder {
        ImageView mImageView;
        ImageView mCancelView;

        ViewHolder(View view) {
            mImageView = (ImageView) view.findViewById(R.id.iv_adapter_select_image_grid_item);
            mCancelView = (ImageView) view.findViewById(R.id.iv_adapter_select_image_grid_item_cancel);
        }
    }
}